<script setup lang="ts">
import { ref } from 'vue'

interface Blog {
  id: number
  title: string
  content: string
  excerpt: string
  author: string
  publishDate: string
  tags: string[]
  readTime: number
  views: number
  likes: number
}

interface Props {
  blog: Blog | null
}

const props = defineProps<Props>()

const emit = defineEmits<{
  back: []
}>()

const isLiked = ref(false)
const likeCount = ref(props.blog?.likes || 0)

const formatDate = (dateString: string) => {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

const toggleLike = () => {
  isLiked.value = !isLiked.value
  likeCount.value += isLiked.value ? 1 : -1
}

if (!props.blog) {
  // Handle null blog case
}
</script>

<template>
  <div v-if="blog" class="blog-detail">
    <div class="detail-header">
      <div class="breadcrumb">
        <button @click="emit('back')" class="back-link">← 返回列表</button>
      </div>
      
      <h1 class="detail-title">{{ blog.title }}</h1>
      
      <div class="detail-meta">
        <div class="author-info">
          <span class="author">👤 {{ blog.author }}</span>
          <span class="publish-date">📅 {{ formatDate(blog.publishDate) }}</span>
        </div>
        
        <div class="article-stats">
          <span class="read-time">⏱️ {{ blog.readTime }} 分钟阅读</span>
          <span class="views">👁️ {{ blog.views }} 阅读</span>
        </div>
      </div>
      
      <div class="tags">
        <span 
          v-for="tag in blog.tags" 
          :key="tag"
          class="tag"
        >
          {{ tag }}
        </span>
      </div>
    </div>
    
    <div class="detail-content">
      <div class="content-body">
        <p class="content-text">{{ blog.content }}</p>
        
        <!-- Extended content for demo -->
        <div class="extended-content">
          <h2>深入探讨</h2>
          <p>在实际开发中，我们经常会遇到各种挑战和问题。通过不断的实践和总结，我们可以积累宝贵的经验，提升自己的技术水平。</p>
          
          <h3>最佳实践</h3>
          <ul>
            <li>保持代码的简洁性和可读性</li>
            <li>遵循团队约定的编码规范</li>
            <li>及时进行代码审查和重构</li>
            <li>编写高质量的单元测试</li>
          </ul>
          
          <h3>总结</h3>
          <p>通过本文的学习，相信大家对相关技术有了更深入的理解。在实际项目中，要根据具体情况选择合适的技术方案，并持续优化和改进。</p>
        </div>
      </div>
      
      <div class="content-actions">
        <button 
          @click="toggleLike"
          :class="['like-btn', { liked: isLiked }]"
        >
          {{ isLiked ? '❤️' : '🤍' }} {{ likeCount }}
        </button>
        
        <button class="share-btn">
          📤 分享
        </button>
        
        <button class="bookmark-btn">
          🔖 收藏
        </button>
      </div>
    </div>
    
    <div class="related-posts">
      <h3>相关文章</h3>
      <div class="related-list">
        <div class="related-item">
          <h4>Vue 3 性能优化技巧</h4>
          <p>探索 Vue 3 的性能优化最佳实践</p>
        </div>
        <div class="related-item">
          <h4>现代前端工程化实践</h4>
          <p>从零开始构建现代前端开发环境</p>
        </div>
        <div class="related-item">
          <h4>JavaScript 高级特性解析</h4>
          <p>深入理解 JavaScript 的核心概念</p>
        </div>
      </div>
    </div>
  </div>
  
  <div v-else class="error-state">
    <h2>文章未找到</h2>
    <p>抱歉，您要查看的文章不存在或已被删除。</p>
    <button @click="emit('back')" class="back-btn">返回列表</button>
  </div>
</template>

<style scoped>
.blog-detail {
  padding: 24px;
  max-width: 800px;
  margin: 0 auto;
}

.detail-header {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 2px solid #f0f0f0;
}

.breadcrumb {
  margin-bottom: 16px;
}

.back-link {
  background: none;
  border: none;
  color: #667eea;
  cursor: pointer;
  font-size: 14px;
  text-decoration: underline;
  padding: 0;
}

.back-link:hover {
  color: #764ba2;
}

.detail-title {
  margin: 0 0 20px 0;
  font-size: 32px;
  font-weight: 700;
  color: #333;
  line-height: 1.3;
}

.detail-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.author-info {
  display: flex;
  gap: 16px;
  font-size: 14px;
  color: #666;
}

.article-stats {
  display: flex;
  gap: 16px;
  font-size: 14px;
  color: #666;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
}

.detail-content {
  margin-bottom: 40px;
}

.content-body {
  margin-bottom: 32px;
}

.content-text {
  font-size: 16px;
  line-height: 1.8;
  color: #333;
  margin-bottom: 24px;
}

.extended-content h2 {
  font-size: 24px;
  color: #333;
  margin: 32px 0 16px 0;
  padding-bottom: 8px;
  border-bottom: 1px solid #e0e0e0;
}

.extended-content h3 {
  font-size: 20px;
  color: #333;
  margin: 24px 0 12px 0;
}

.extended-content p {
  font-size: 16px;
  line-height: 1.8;
  color: #555;
  margin-bottom: 16px;
}

.extended-content ul {
  margin: 16px 0;
  padding-left: 24px;
}

.extended-content li {
  font-size: 16px;
  line-height: 1.8;
  color: #555;
  margin-bottom: 8px;
}

.content-actions {
  display: flex;
  gap: 12px;
  padding: 20px 0;
  border-top: 1px solid #e0e0e0;
  border-bottom: 1px solid #e0e0e0;
}

.like-btn,
.share-btn,
.bookmark-btn {
  padding: 10px 20px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 6px;
}

.like-btn:hover,
.share-btn:hover,
.bookmark-btn:hover {
  background: #f8f9ff;
  border-color: #667eea;
  color: #667eea;
}

.like-btn.liked {
  background: #ff6b6b;
  color: white;
  border-color: #ff6b6b;
}

.related-posts {
  margin-top: 40px;
  padding-top: 32px;
  border-top: 2px solid #f0f0f0;
}

.related-posts h3 {
  margin: 0 0 20px 0;
  font-size: 20px;
  color: #333;
}

.related-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.related-item {
  padding: 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.related-item:hover {
  background: #f8f9ff;
  border-color: #667eea;
  transform: translateY(-2px);
}

.related-item h4 {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #333;
}

.related-item p {
  margin: 0;
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}

.error-state {
  text-align: center;
  padding: 60px 20px;
}

.error-state h2 {
  margin: 0 0 16px 0;
  color: #333;
}

.error-state p {
  margin: 0 0 24px 0;
  color: #666;
}

.back-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
}

@media (max-width: 768px) {
  .blog-detail {
    padding: 16px;
  }
  
  .detail-title {
    font-size: 24px;
  }
  
  .detail-meta {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .content-actions {
    flex-wrap: wrap;
  }
  
  .related-list {
    grid-template-columns: 1fr;
  }
}
</style>
